//
//  SSWelcomeViewController.swift
//  weibo
//
//  Created by 邵帅 on 16/6/26.
//  Copyright © 2016年 groupNineObject. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class SSWelcomeViewController: UIViewController {
    
    override func loadView() {
        
        view = bgImgView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUIs()
    }
    
    // 显示之后再开启动画
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    // 头像动画
    private func startAnimation() {
        
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 1.0, initialSpringVelocity: 10, options: [], animations: {
            
            self.iconImgView.snp_updateConstraints(closure: { (make) in
                
                make.top.equalTo(self.view.snp_bottom).offset(-UIScreen.mainScreen().bounds.height + 100)
            })
            
            // 约束动画需要下面的方法才会懂
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
            // 动画结束后，进行界面跳转
            NSNotificationCenter.defaultCenter().postNotificationName(kNotificationChanged, object: self)
        }
        
        
    }
    
    
    func setupUIs() {
        
        view.addSubview(iconImgView)
        view.addSubview(messageLbl)
        
        // 约束
        iconImgView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(view.snp_bottom).offset(-250)
        }
        
        messageLbl.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view.snp_centerX)
            make.top.equalTo(iconImgView.snp_bottom).offset(10)
        }
        
        let urlString = SSOauthModel.shareInstance.userModel?.profile_image_url
        
        iconImgView.sd_setImageWithURL(NSURL(string: urlString!), placeholderImage: UIImage(named: "avatar_default_big"))
   
            }

    // 懒加载控件
    private lazy var bgImgView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconImgView: UIImageView = {
       
        let imgView = UIImageView()
        
        imgView.image = UIImage(named: "avatar_default_big")
        
//        imgView.layer.cornerRadius = 85 / 2.0
        imgView.layer.cornerRadius = 25
        imgView.layer.masksToBounds = true
        imgView.layer.borderWidth = 1.0
        imgView.layer.borderColor = UIColor.yellowColor().CGColor
        imgView.sizeToFit()
        
        return imgView
    }()

    private lazy var messageLbl: UILabel = {
        
        let lbl = UILabel()
        
        lbl.text = "欢迎归来"
        lbl.font = UIFont.systemFontOfSize(15.0)
        lbl.textAlignment = .Center
        lbl.textColor = UIColor.darkGrayColor()
        
        return lbl
    }()
}
